//
//  JCHomeViewController.swift
//  swiftWeibo
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class JCHomeViewController: JCBaseTableViewController {

    
    var statuses :[JCStatus]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 判断用户已经登录,才需要做下面的操作
        if !userlogin {
            return
        }
        
        setupNav()

        // 1.调用微博模型来加载微博数据(微博模型),
        // 2.准备闭包
        JCStatus.mLoadStatus { (statuses, error) -> () in
            // 7.1 判断错误
            if error != nil {
                print("加载微博数据出错")
                return
            }
            
            print("statuses: \(statuses)")
            
            // 7.2 将微博正文内容显示出来, 赋值给 控制器的statuses属性
            self.statuses = statuses
            
            // 7.3 刷新数据
            self.tableView.reloadData()
        }
        
        
        
    }
    // MARK: - 设置导航栏
    private func setupNav() {
        // 设置导航栏左边
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        
        // 设置导航栏右边
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        
        // 设置标题
        let title = JCUserAccount.loadAccount()?.screen_name ?? "没获取到用户名"
        
        // 创建title按钮
        let titleButton = JCTitleButton(imageName: "navigationbar_arrow_down", title: title)
        
        // 添加点击事件
        titleButton.addTarget(self, action: "homeTitleButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleButton

    }
    /// 标题按钮点击事件
    func homeTitleButtonClick(button: UIButton) {
        // 1.设置按钮的状态
        button.selected = !button.selected
        // 抽取代码
        UIView.animateWithDuration(0.25) { () -> Void in
            // 根据按钮的选中状态来确定是旋转方向,如果按钮选中,转180°少一点,否则转到默认位置
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        animationStart(self)
    }
    
    func animationStart (viewController: UIViewController) {
        let animate = CATransition()
        animate.type = "rippleEffect"
        animate.subtype = kCATransitionFromRight
        animate.duration = 1.0
        viewController.view.layer .addAnimation(animate, forKey: nil)
    }
//
    
    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }

    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
